		TITLE LIBREADT - Copyright (c) 1994 by SLR Systems

		INCLUDE	MACROS
if	fgh_inthreads
		INCLUDE	IO_STRUC
		INCLUDE	LIBRARY
		INCLUDE	WIN32DEF

;		PUBLIC	LIBREADT


		.DATA

		EXTERNDEF	LIB_REQUEST_ORDER_SEM:GLOBALSEM_STRUCT,LIBRARY_GARRAY:STD_PTR_S,_FILE_LIST_GARRAY:STD_PTR_S
		EXTERNDEF	TCONVERT_SEM:GLOBALSEM_STRUCT


		.CODE	PASS1_TEXT

		EXTERNDEF	_capture_eax:proc
		EXTERNDEF	_release_eax:proc
		EXTERNDEF	_release_eax_bump:proc
		EXTERNDEF	_get_new_phys_blk:proc,_terr_abort:proc,XCNOTIFY:proc
		externdef	_lerr_abort:proc

		EXTERNDEF	DOS_READ_ERR:ABS,DOS_POS_ERR:ABS,UNEXP_XEOF_ERR:ABS


;LIBREADT	PROC
;		;
;		;THIS GUY READS LIBRARY FILES
;		;
;		;MAKE THIS SLIGHTLY HIGHER PRIORITY THAN MAIN
;		;
;		MOV	EBX,[ESP+4]
;
;		push	-1
;		push	0
;		PUSH	OFF XCNOTIFY
;		ASSUME	FS:NOTHING
;
;		PUSH	DWORD PTR FS:[0]
;		MOV	FS:[0],ESP
;
;		PUSH	THREAD_PRIORITY_ABOVE_NORMAL	;WAS NORMAL
		ASSUME	EBX:PTR MYL2_STRUCT
;
;		MOV	EAX,[EBX].MYL2_LIBREAD_THREAD_HANDLE
;
;		PUSH	EAX
;		CALL	SetThreadPriority	;I DON'T CARE IF THIS FAILS
;L1$:
;		LEA	EAX,[EBX].MYL2_LIB_BLOCK_SEM	;WAIT TILL A BLOCK IS REQUESTED
;		push	EAX
;		call	_capture_eax
;		add	ESP,4
;
;		BITT	LIBS_DONE		;DONE WITH LIBS?
;		JNZ	L2$
;
;		CALL	READ_A_BLOCK		;FIND AND READ THE REQUESTED BLOCK
;		JMP	L1$
;
;L2$:
READ_LIBT_ABORT	proc
		;
		;NOW, TERMINATE THIS THREAD
		;
		PUSH	0
		CALL	ExitThread 		;STOP THIS THREAD

READ_LIBT_ABORT	ENDP

;		public	_read_a_block
;_read_a_block:
;		mov	EBX,4[ESP]

		public	READ_A_BLOCK
READ_A_BLOCK	PROC	NEAR
		;
		;SCAN MYL_REQUESTED LOOKING FOR A LIBRARY WITH A DESIRED BLOCK
		;
		push	offset LIB_REQUEST_ORDER_SEM	;LIBRARY REQUEST ORDER CANNOT CHANGE
		call	_capture_eax
		add	ESP,4

		MOV	ECX,[EBX].MYL2_FIRST_REQUEST_LIB_GINDEX
		JMP	L2$

		ASSUME	ECX:PTR LIBRARY_STRUCT
L1$:
		MOV	ECX,[ECX]._LS_NEXT_TRLIB_GINDEX
L2$:
		TEST	ECX,ECX
		JZ	L3$				;SHOULD NOT HAPPEN..., BUT MIGHT

		MOV	EDX,ECX
		;CONVERT	ECX,ECX,LIBRARY_GARRAY
		MOV	EAX,[ECX]._LS_OS2_BLOCKS_REQUESTED

		TEST	EAX,EAX
		JZ	L1$
L3$:
		push	ECX
		push	offset LIB_REQUEST_ORDER_SEM
		call	_release_eax
		add	ESP,4
		pop	ECX

		BITT	LIBS_DONE
		JNZ	READ_LIBT_ABORT

		TEST	ECX,ECX
		JZ	L9$

		MOV	[EBX].MYL2_CURRENT_LIB_GINDEX,EDX
		MOV	EAX,ECX

		JMP	READ_THIS_LIBRARY

L9$:
		RET

READ_A_BLOCK	ENDP


LPOS_ERROR:
		MOV	AL,DOS_POS_ERR
LERR_ABORT:
		push	EBX
		push	EAX
		call	_lerr_abort

;		MOV	ECX,[EBX].MYL2_CURRENT_LIB_GINDEX
;
;		BITT	LIBS_DONE
;		JNZ	READ_LIBT_ABORT
;
;		CONVERT	ECX,ECX,LIBRARY_GARRAY
;		ASSUME	ECX:PTR LIBRARY_STRUCT
;
;		MOV	ECX,[ECX]._LS_FILE_LIST_GINDEX
;
;		CONVERT	ECX,ECX,_FILE_LIST_GARRAY
;
;		ADD	ECX,FILE_LIST_STRUCT.FILE_LIST_NFN.NFN_TEXT
;		push	ECX
;		push	EAX
;		call	_terr_abort
;		add	ESP,8

;		public	_read_this_library
;_read_this_library:
;		mov	EAX,8[ESP]
;		mov	EBX,4[ESP]

READ_THIS_LIBRARY	PROC	NEAR
		;
		;EAX - THIS LIB HAS AT LEAST ONE OUTSTANDING BLOCK REQUEST, READ THE FIRST ONE...
		;
		MOV	ESI,EAX
		CALL	CAPTURE_VERIFY_HANDLE		;ASSURE EXCLUSIVE ACCESS TO HANDLE
		ASSUME	ESI:PTR LIBRARY_STRUCT

		MOV	EDI,[ESI]._LS_NEXT_TRLIB_BLOCK	;NEXT BLOCK FOR THIS THREAD TO READ

		ADD	[ESI]._LS_NEXT_TRLIB_BLOCK,4	;BLOCK TO READ NEXT TIME
		MOV	EAX,[EDI]			;BLOCK # TO READ
		;
		;ESI IS LIBRARY LOGICAL, ECX IS BLOCK #
		;
		;
		;MAKE SURE DOSFILEPOSITION IS CORRECT
		;
		MOV	EDI,EAX

		SHL	EAX,PAGE_BITS
		MOV	ECX,[ESI]._LS_FILE_POSITION

		CMP	EAX,ECX
		JZ	L3$

		PUSH	FILE_BEGIN
		PUSH	0			;NO HIGH WORD

		MOV	ECX,[ESI]._LS_HANDLE
		PUSH	EAX			;DISTANCE TO MOVE

		PUSH	ECX			;FILE HANDLE
		CALL	SetFilePointer

		CMP	EAX,-1
		JZ	LPOS_ERROR

		MOV	[ESI]._LS_FILE_POSITION,EAX
L3$:
		PUSH	EAX		;ROOM FOR RESULT
		MOV	EDX,ESP

		PUSH	0		;NOT OVERLAPPED
		PUSH	EDX		;RESULT
		;
		;CALCULATE # OF BYTES
		;
		MOV	EAX,[ESI]._LS_BLOCKS
		LEA	ECX,[EDI+1]

		CMP	EAX,ECX
		JNZ	L39$

		MOV	EAX,[ESI]._LS_FILE_LIST_GINDEX
		CONVERT	EAX,EAX,_FILE_LIST_GARRAY
		ASSUME	EAX:PTR FILE_LIST_STRUCT

		MOV	EAX,[EAX].FILE_LIST_NFN.NFN_FILE_LENGTH

		AND	EAX,PAGE_SIZE-1
		JNZ	L4$
L39$:
		MOV	EAX,PAGE_SIZE
L4$:
		PUSH	EAX
		MOV	[EBX].MYL2_DESIRED_BYTES,EAX
		;
		;ALLOCATE LOGICAL BLOCK
		;
		MOV	ECX,[ESI]._LS_HANDLE	;FILE HANDLE

		push	ECX
		push	EDX
		call	_get_new_phys_blk
		pop	EDX
		pop	ECX

		BITT	LIBS_DONE
		JNZ	READ_LIBT_ABORT

		PUSH	EAX
		PUSH	ECX

		MOV	[EBX].MYL2_BUFFER_ADDRESS,EAX
		CALL	ReadFile

		TEST	EAX,EAX
		JZ	LREAD_ERROR

		POP	EAX			;# READ
		MOV	ECX,[EBX].MYL2_DESIRED_BYTES

		CMP	EAX,ECX
		JNZ	LEOF_ERROR

		ADD	[ESI]._LS_FILE_POSITION,EAX
;		LEA	EAX,[ESI]._LS_HANDLE_SEM	;HANDLE AVAILABLE TO HANDLE_CHECK

		MOV	ECX,[EBX].MYL2_BUFFER_ADDRESS
		;
		;STORE BLOCK ADDRESS
		;
		MOV	EDX,[ESI]._LS_BLOCK_TABLE
		MOV	EAX,[ESI]._LS_BLOCKS

		CMP	EAX,PAGE_SIZE/8/2
		JAE	L6$
L69$:
		DEC	[ESI]._LS_OS2_BLOCKS_REQUESTED
		MOV	[EDX+EDI*8],ECX			;STORE BLOCK SEGMENT

		LEA	EAX,[EBX].MYL2_BLOCK_READ_SEM	;SIGNAL ANOTHER COMPLETION
		push	EAX
		call	_release_eax
		add	ESP,4
		ret

LREAD_ERROR:
		MOV	AL,DOS_READ_ERR
		JMP	LERR_ABORT

LEOF_ERROR:
		MOV	AL,UNEXP_XEOF_ERR
		JMP	LERR_ABORT

L6$:
		;
		;THERE ARE PAGE_SIZE/8 BLOCKS PER PAGE
		;
		MOV	EAX,EDI

		SHR	EAX,PAGE_BITS-3
		AND	EDI,(PAGE_SIZE-1) SHR 3

		MOV	EDX,[ESI+EAX*4]._LS_BLOCK_TABLE
		JMP	L69$

READ_THIS_LIBRARY	ENDP


CAPTURE_VERIFY_HANDLE	PROC	NEAR
		;
		;ESI IS LIBRARY
		;

		MOV	EAX,[ESI]._LS_HANDLE

		TEST	EAX,EAX
		JZ	L1$

		RET

L1$:
		;
		;SOMEONE HAS CLOSED THIS HANDLE, REOPEN IT PLEASE
		;
		MOV	AL,0
		CALL	LERR_ABORT

		RET

CAPTURE_VERIFY_HANDLE	ENDP

endif

		END

